import { RequestRs } from '../../modules/Request-rs';
import { Component, OnInit } from '@angular/core';

import {HttpService} from '../../share/service/http.service';
import {User} from '../../modules/user';
import {HttpParams} from '@angular/common/http';
import {ReponseRs} from '../../modules/Response-rs';
import { HttpAssist } from '../../share/assist/http-assist';
import { Router } from '@angular/router';

@Component({
  selector: 'app-register',
  templateUrl: './register.component.html',
  styleUrls: ['./register.component.css']
})
export class RegisterComponent implements OnInit {
  req: RequestRs = new RequestRs();
  res: ReponseRs = new ReponseRs();
  user: User = new User();
  constructor(private httpService: HttpService,private router: Router) { }

  ngOnInit() {
    // this.users = this.userService.getUsers();
  }

  onRegister(){
    const serviceName = 'register';
    this.req.body = JSON.stringify(this.user);
    const message = JSON.stringify(this.req);
    this.httpService.postByJson<ReponseRs>(serviceName,HttpAssist.POST_BODY,message).subscribe(
      s => {
        this.res = s as ReponseRs;
        console.info(serviceName + ' ' + this.res.status);
        console.info(this.res.message);
        if (this.res.status === HttpAssist.RS_SUCCESS) {
          this.user =  JSON.parse(this.res.message);
          alert('registration is successfully, go to login page');
          this.router.navigate(['/login']);
        }else {
          alert('fail, detail{' + 'code:' + this.res.status + 'message:'+ this.res.message+'}');
        }
      },
      error => {
          alert('fail,http error in UI, detail' + error);
      }
    );
  }
  goLogin(){
    this.router.navigate(['/login']);
  }
  get diagnostic() { return JSON.stringify(this.user); }
}
